/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    fdcan.c
  * @brief   This file provides code for the configuration
  *          of the FDCAN instances.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "fdcan.h"

/* USER CODE BEGIN 0 */
#include<string.h>
#include "nfc_read_card.h"
#include "nfc_ctrl.h"
#include "rte.h"
#include "ver_comp.h"
static FDCAN_FilterTypeDef FDCAN1_RXFilter;
/* USER CODE END 0 */

FDCAN_HandleTypeDef hfdcan1;

/* FDCAN1 init function */
void MX_FDCAN1_Init(void)
{

  /* USER CODE BEGIN FDCAN1_Init 0 */

  /* USER CODE END FDCAN1_Init 0 */

  /* USER CODE BEGIN FDCAN1_Init 1 */

  /* USER CODE END FDCAN1_Init 1 */
  hfdcan1.Instance = FDCAN1;
  hfdcan1.Init.FrameFormat = FDCAN_FRAME_CLASSIC;
  hfdcan1.Init.Mode = FDCAN_MODE_NORMAL;
  hfdcan1.Init.AutoRetransmission = DISABLE;
  hfdcan1.Init.TransmitPause = DISABLE;
  hfdcan1.Init.ProtocolException = DISABLE;
  hfdcan1.Init.NominalPrescaler = 2;
  hfdcan1.Init.NominalSyncJumpWidth = 2;
  hfdcan1.Init.NominalTimeSeg1 = 12;
  hfdcan1.Init.NominalTimeSeg2 = 3;
  hfdcan1.Init.DataPrescaler = 2;
  hfdcan1.Init.DataSyncJumpWidth = 2;
  hfdcan1.Init.DataTimeSeg1 = 12;
  hfdcan1.Init.DataTimeSeg2 = 3;
  hfdcan1.Init.MessageRAMOffset = 0;
  hfdcan1.Init.StdFiltersNbr = 4;
  hfdcan1.Init.ExtFiltersNbr = 0;
  hfdcan1.Init.RxFifo0ElmtsNbr = 32;
  hfdcan1.Init.RxFifo0ElmtSize = FDCAN_DATA_BYTES_8;
  hfdcan1.Init.RxFifo1ElmtsNbr = 0;
  hfdcan1.Init.RxFifo1ElmtSize = FDCAN_DATA_BYTES_8;
  hfdcan1.Init.RxBuffersNbr = 0;
  hfdcan1.Init.RxBufferSize = FDCAN_DATA_BYTES_8;
  hfdcan1.Init.TxEventsNbr = 0;
  hfdcan1.Init.TxBuffersNbr = 0;
  hfdcan1.Init.TxFifoQueueElmtsNbr = 16;
  hfdcan1.Init.TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION;
  hfdcan1.Init.TxElmtSize = FDCAN_DATA_BYTES_8;
  if (HAL_FDCAN_Init(&hfdcan1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN FDCAN1_Init 2 */
	//配置RX滤波�??????   
    FDCAN1_RXFilter.IdType=FDCAN_STANDARD_ID;                       //标准ID
    FDCAN1_RXFilter.FilterIndex=0;                                  //滤波器索�??????                   
    FDCAN1_RXFilter.FilterType=FDCAN_FILTER_MASK;                   //滤波器类�??????
    FDCAN1_RXFilter.FilterConfig=FDCAN_FILTER_TO_RXFIFO0;           //过滤�??????0关联到FIFO0  
    FDCAN1_RXFilter.FilterID1=CAN_CTRL_ID;                               //32位ID
    FDCAN1_RXFilter.FilterID2=0x7ff;                               //如果FDCAN配置为传统模式的话，这里�??????32位掩�??????
    if(HAL_FDCAN_ConfigFilter(&hfdcan1,&FDCAN1_RXFilter)!=HAL_OK) //滤波器初始化
		{
			Error_Handler();
		}
    	//配置RX滤波�??????   
    FDCAN1_RXFilter.IdType=FDCAN_STANDARD_ID;                       //标准ID
    FDCAN1_RXFilter.FilterIndex=1;                                  //滤波器索�??????                   
    FDCAN1_RXFilter.FilterType=FDCAN_FILTER_MASK;                   //滤波器类�??????
    FDCAN1_RXFilter.FilterConfig=FDCAN_FILTER_TO_RXFIFO0;           //过滤�??????0关联到FIFO0  
    FDCAN1_RXFilter.FilterID1=CAN_REC_ID;                               //32位ID
    FDCAN1_RXFilter.FilterID2=0x7ff;                               //如果FDCAN配置为传统模式的话，这里�??????32位掩�??????
    if(HAL_FDCAN_ConfigFilter(&hfdcan1,&FDCAN1_RXFilter)!=HAL_OK) //滤波器初始化
		{
			Error_Handler();
		}
    	//配置RX滤波�??????   
    FDCAN1_RXFilter.IdType=FDCAN_STANDARD_ID;                       //标准ID
    FDCAN1_RXFilter.FilterIndex=2;                                  //滤波器索�??????                   
    FDCAN1_RXFilter.FilterType=FDCAN_FILTER_MASK;                   //滤波器类�??????
    FDCAN1_RXFilter.FilterConfig=FDCAN_FILTER_TO_RXFIFO0;           //过滤�??????0关联到FIFO0  
    FDCAN1_RXFilter.FilterID1=VER_COMP_ID;                               //32位ID
    FDCAN1_RXFilter.FilterID2=0x7ff;                               //如果FDCAN配置为传统模式的话，这里�??????32位掩�??????
    if(HAL_FDCAN_ConfigFilter(&hfdcan1,&FDCAN1_RXFilter)!=HAL_OK) //滤波器初始化
		{
			Error_Handler();
		}
        	//配置RX滤波�??????   
    FDCAN1_RXFilter.IdType=FDCAN_STANDARD_ID;                       //标准ID
    FDCAN1_RXFilter.FilterIndex=3;                                  //滤波器索�??????                   
    FDCAN1_RXFilter.FilterType=FDCAN_FILTER_MASK;                   //滤波器类�??????
    FDCAN1_RXFilter.FilterConfig=FDCAN_FILTER_TO_RXFIFO0;           //过滤�??????0关联到FIFO0  
    FDCAN1_RXFilter.FilterID1=CAN_SWIPE_ID;                               //32位ID
    FDCAN1_RXFilter.FilterID2=0x7ff;                               //如果FDCAN配置为传统模式的话，这里�??????32位掩�??????
    if(HAL_FDCAN_ConfigFilter(&hfdcan1,&FDCAN1_RXFilter)!=HAL_OK) //滤波器初始化
		{
			Error_Handler();
		}
      if (HAL_FDCAN_ConfigGlobalFilter(&hfdcan1, FDCAN_REJECT, FDCAN_REJECT, FDCAN_FILTER_REMOTE, FDCAN_FILTER_REMOTE) != HAL_OK)
  {
    Error_Handler();
  }
		/**************** 以下为启动中�?????? ****************/
  HAL_FDCAN_ActivateNotification(&hfdcan1, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0); // 使能FIFO0数据接收中断
  HAL_FDCAN_ActivateNotification(&hfdcan1, FDCAN_IT_BUS_OFF, 0);
		HAL_GPIO_WritePin(GPIOE,CAN_Silent_O_PE10_Pin,GPIO_PIN_RESET);/*使能CAN*/
		 HAL_FDCAN_Start(&hfdcan1);                               //�??????启FDCAN 
  /* USER CODE END FDCAN1_Init 2 */

}

void HAL_FDCAN_MspInit(FDCAN_HandleTypeDef* fdcanHandle)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
  if(fdcanHandle->Instance==FDCAN1)
  {
  /* USER CODE BEGIN FDCAN1_MspInit 0 */

  /* USER CODE END FDCAN1_MspInit 0 */

  /** Initializes the peripherals clock
  */
    PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_FDCAN;
    PeriphClkInitStruct.FdcanClockSelection = RCC_FDCANCLKSOURCE_HSE;
    if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
    {
      Error_Handler();
    }

    /* FDCAN1 clock enable */
    __HAL_RCC_FDCAN_CLK_ENABLE();

    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**FDCAN1 GPIO Configuration
    PA11     ------> FDCAN1_RX
    PA12     ------> FDCAN1_TX
    */
    GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    GPIO_InitStruct.Alternate = GPIO_AF9_FDCAN1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    /* FDCAN1 interrupt Init */
    HAL_NVIC_SetPriority(FDCAN1_IT0_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(FDCAN1_IT0_IRQn);
  /* USER CODE BEGIN FDCAN1_MspInit 1 */

  /* USER CODE END FDCAN1_MspInit 1 */
  }
}

void HAL_FDCAN_MspDeInit(FDCAN_HandleTypeDef* fdcanHandle)
{

  if(fdcanHandle->Instance==FDCAN1)
  {
  /* USER CODE BEGIN FDCAN1_MspDeInit 0 */

  /* USER CODE END FDCAN1_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_FDCAN_CLK_DISABLE();

    /**FDCAN1 GPIO Configuration
    PA11     ------> FDCAN1_RX
    PA12     ------> FDCAN1_TX
    */
    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11|GPIO_PIN_12);

    /* FDCAN1 interrupt Deinit */
    HAL_NVIC_DisableIRQ(FDCAN1_IT0_IRQn);
  /* USER CODE BEGIN FDCAN1_MspDeInit 1 */

  /* USER CODE END FDCAN1_MspDeInit 1 */
  }
}

/* USER CODE BEGIN 1 */


/**
  * 函数功能: FDCAN2启动
  * 输入参数: uint8_t  canordernumber  CAN1 �??????1 CAN2�??????2   
  * 输入参数: uint8_t  sendcanid  发�?�数据的canid 
	* 输入参数: uint8_t* msg  发�?�数据的数组
	* 输入参数: uint32_t len  发�?�数据的长度
  * 返回值：  uint8_t  成功返回数据1 失败返回数据0
  * 说明:     
  */
uint8_t FDCAN_Send_Msg(uint32_t id,uint8_t* msg,uint32_t len)
{	
	  static uint8_t msgdata[8]={0};
		FDCAN_TxHeaderTypeDef fdcan_TxHeader;
		memcpy(msgdata,msg,len);
    fdcan_TxHeader.Identifier=id;                      				//32位ID
    fdcan_TxHeader.IdType=FDCAN_STANDARD_ID;                  //标准ID
    fdcan_TxHeader.TxFrameType=FDCAN_DATA_FRAME;              //数据�??????
    fdcan_TxHeader.DataLength=FDCAN_DLC_BYTES_8;              //数据长度
    fdcan_TxHeader.ErrorStateIndicator=FDCAN_ESI_ACTIVE;            
    fdcan_TxHeader.BitRateSwitch=FDCAN_BRS_OFF;               //关闭速率切换
    fdcan_TxHeader.FDFormat=FDCAN_CLASSIC_CAN;                //传统的CAN模式
    fdcan_TxHeader.TxEventFifoControl=FDCAN_NO_TX_EVENTS;     //无发送事�??????
    fdcan_TxHeader.MessageMarker=0;                           
    
				if(HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan1,&fdcan_TxHeader,msgdata)!=HAL_OK) return 1;//发�??

    return 0;	
}

/**************** 以下为重写中断回调函�?????? ****************/
// Fifo0收到消息回调

void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo0ITs)
{
    if (hfdcan == &hfdcan1) // 判断是hfdcan1的中�??????
    {
        if ((RxFifo0ITs & FDCAN_IT_RX_FIFO0_NEW_MESSAGE) != RESET) // 判断是FIFO0_NEW_MESSAGE回调
        {
            FDCAN_RxHeaderTypeDef   RxHeader; // 用来保存接收到的数据帧头部信�??????
            uint8_t                 RxData[8]; // 用来保存接收数据端数�??????

            if(HAL_FDCAN_GetRxMessage(&hfdcan1, FDCAN_RX_FIFO0, &RxHeader, RxData) == HAL_OK) // 从接收队列中读取数据�??????
            {
              if (CAN_CTRL_ID == RxHeader.Identifier )
              {
                NFCCTRL_can_cbk(RxHeader.Identifier,RxData);
              }else if (CAN_REC_ID == RxHeader.Identifier || CAN_SWIPE_ID == RxHeader.Identifier)
              {
                NFCRC_read_card_cbk(RxHeader.Identifier,RxData);
              }else if (VER_COMP_ID == RxHeader.Identifier)
              {
                ver_comp_cancbk(8,RxData);
              }
              
                HAL_FDCAN_ActivateNotification(hfdcan, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0);   // 使能FIFO0数据接收中断
								
								//FDCAN_Send_Msg(0x7ba,RxData,8);/*发�?�测�??????*/
            }
        }
    }
}
	 void HAL_FDCAN_ErrorStatusCallback(FDCAN_HandleTypeDef *hfdcan, uint32_t ErrorStatusITs)
{
		MX_FDCAN1_Init();
}
/* USER CODE END 1 */
